const { ccclass, property } = cc._decorator;

@ccclass
export default class Scene extends cc.Component {

    @property(cc.Node)
    btnTitle: cc.Node = null;

    protected _btnBack: cc.Node = null;
    protected _titleNode: cc.Node = null;
    protected _title: cc.Label = null;
    protected _rootScene: string;

    protected onLoad(): void {
        this._rootScene = "Hall";
        this._btnBack = this.btnTitle.getChildByName("btnBack");
        this._titleNode = this.btnTitle.getChildByName("title");
        this._title = this._titleNode.getComponent(cc.Label);
    }

    protected onEnable(): void {
        let scene: string = cc.director.getScene().name;
        this._title.string = scene;
        this._btnBack.active = scene != this._rootScene;
        if (this._btnBack) {
            this._btnBack.on(cc.Node.EventType.TOUCH_END, this.onBack, this);
        }
    }

    protected onDisable(): void {
        if (this._btnBack) {
            this._btnBack.off(cc.Node.EventType.TOUCH_END, this.onBack, this);
        }
    }

    private onBack(): void {
        this.gotoScene(this._rootScene);
    }

    protected gotoScene(scene: string): void {
        console.log("gotoScene: ", scene);
        if (scene) {
            cc.director.loadScene(scene);
        }
    }
}
